// pages/login/login.js
const app = getApp()
Page({
     getUserInfo: function () {
          var that = this
          wx.login({
               success: function (res) {
                    console.log(res.code)
                    var code = res.code
                    wx.getUserInfo({
                         success: res => {
                              var userbox = res.userInfo
                              var userimg=res.userInfo.avatarUrl
                              var username=res.userInfo.nickName
                              wx.request({
                                   url: app.data.apiurl +'/ddk/user',
                                   data: {
                                        userCode: code,
                                        userImageUrl: userimg,
                                        userName: username
                                   },
                                   header: {
                                        'content-type': 'application/json' // 默认值
                                   },
                                   method: 'post',
                                   dataType: 'json',
                                   success: function(res) {
                                        wx.reLaunch({
                                             url: '/pages/index/index',
                                        })
                                        console.log(res.data.data.openid)
                                        wx.setStorageSync("userid", res.data.data.openid)
                                   },
                                   fail: function(res) {
                                        wx.showToast({
                                             title: '网络异常',
                                             icon: 'none',
                                             duration: 2000
                                        })
                                   }
                              })
                         }
                    })
               },
               fail:function(){
                    wx.reLaunch({
                         url: '/pages/login/login',
                    })
               }
          })
     }
})